package com.example.educationai.controller;

import com.example.educationai.entity.SimpleUser;
import com.example.educationai.entity.SimpleUser.UserRole;
import com.example.educationai.service.SimpleUserService;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

@Slf4j
@Controller
public class LoginController {

    @Autowired
    private SimpleUserService simpleUserService;

    @GetMapping("/simple-login")
    public String showLoginForm() {
        return "simple-login";
    }

    @PostMapping("/simple-login")
    public String login(@RequestParam String username,
                       @RequestParam String password,
                       HttpSession session,
                       RedirectAttributes redirectAttributes) {
        try {
            log.debug("尝试登录用户: {}", username);
            SimpleUser user = simpleUserService.login(username, password);
            session.setAttribute("user", user);
            session.setAttribute("studentName", user.getUsername());
            
            // 根据用户角色重定向到不同的页面
            if (UserRole.TEACHER.equals(user.getRole())) {
                return "redirect:/class/manage";
            } else {
                return "redirect:/exam/list";
            }
        } catch (Exception e) {
            log.error("登录失败", e);
            redirectAttributes.addFlashAttribute("error", "登录失败：" + e.getMessage());
            return "redirect:/simple-login";
        }
    }

    @GetMapping("/simple-register")
    public String showRegisterForm() {
        return "simple-register";
    }

    @PostMapping("/simple-register")
    public String register(@RequestParam String username,
                          @RequestParam String password,
                          @RequestParam String confirmPassword,
                          @RequestParam String email,
                          @RequestParam UserRole role,
                          RedirectAttributes redirectAttributes) {
        try {
            log.debug("开始处理注册请求 - 用户名: {}", username);
            
            // 验证密码
            if (!password.equals(confirmPassword)) {
                log.debug("密码不匹配");
                redirectAttributes.addFlashAttribute("error", "两次输入的密码不一致");
                return "redirect:/simple-register";
            }
            
            SimpleUser user = new SimpleUser();
            user.setUsername(username);
            user.setPassword(password);
            user.setEmail(email);
            user.setRole(role);
            
            simpleUserService.register(user);
            log.debug("用户注册成功: {}", username);
            
            redirectAttributes.addFlashAttribute("success", "注册成功，请登录");
            return "redirect:/simple-login";
        } catch (Exception e) {
            log.error("注册失败", e);
            redirectAttributes.addFlashAttribute("error", "注册失败：" + e.getMessage());
            return "redirect:/simple-register";
        }
    }

    @GetMapping("/logout")
    public String logout(HttpSession session) {
        session.invalidate();
        return "redirect:/simple-login";
    }
} 